<template>
  <div class="grid grid-cols-2 h-screen">
    <div class="col-span-2 order-2 p-10 md:col-span-1 md:order-1 bg-slate-900">
      <div class="flex justify-center items-center h-full flex-col animate__animated animate__bounceInLeft animate__fast">
        <h2 class="font-bold text-4xl mb-7 text-white">web log 博客登录</h2>
        <p class="text-white">spring boot + mybaits plus +vue3.2 + vite 开发的前后端分离项目</p>
        <img src="@/assets/developer.png">

      </div>
    </div>
    <div class="col-span-2 order-1 p-10 md:col-span-1 md:order-1 bg-white">
      <div class="flex justify-center items-center h-full flex-col animate__animated animate__bounceInRight animate__fast">
         <h1 class="font-bold text-4xl mb-5">欢迎回来</h1>
         <div class="flex items-center justify-center mb-7 text-gray-400 space-x-2">
           <!-- 左边横线，高度为 1px, 宽度为 16，背景色设置 -->
           <span class="h-[1px] w-16 bg-gray-200"></span>
           <span>账号密码登录</span>
           <span class="h-[1px] w-16 bg-gray-200"></span>

         </div>

        <el-form class="w-5/6 md:w-2/5" ref="formRef" :rules="rules" :model="form">
            <el-form-item prop="username">
                <el-input size="large" v-model="form.username" placeholder="请输入用户名" :prefix-icon="User" clearable/>
            </el-form-item>
            <el-form-item prop="password">
              <el-input size="large" v-model="form.password" type="password" placeholder="请输入密码" :prefix-icon="Lock" show-password clearable/>
            </el-form-item>
            <el-form-item>
              <el-button class="w-full" size="large" type="primary" :loading="loading" @click="onSubmit">登录</el-button>
            </el-form-item>
        </el-form>
      </div>
    </div>

  </div>
</template>
<script setup>
import {User, Lock} from '@element-plus/icons-vue'
import {login} from "@/api/admin/user";
import {reactive,ref,onMounted,onBeforeUnmount} from "vue";
import {useRouter} from "vue-router";
import {ElMessage} from "element-plus";

import {showMessage} from "@/composables/utils.js";

import {setToken} from "@/composables/cookie.js";

import {userUserStore} from "@/stores/user.js";
const userStore = userUserStore()
const router = useRouter()
//登录按钮加载
const loading = ref(false)
// 定义响应式的表单对象
const form = reactive({
  username: '',
  password: ''
})

//表单引用
const formRef = ref(null)

//校验规则
const rules = {
    username: [
      {
        required: true,
        message: '用户名不能为空',
        trigger: 'blur'
      }
    ],
  password: [
    {
      required: true,
      message: '密码不能为空',
      trigger: 'blur'
    }
  ]
}
// 按回车键后，执行登录事件
function onKeyUp(e){
  console.log(e)
  if(e.key== 'Enter'){
    onSubmit()
  }
}

onMounted(()=>{
  document.addEventListener('keyup',onKeyUp)
})

onBeforeUnmount(()=>{
  document.removeEventListener('keyup',onKeyUp)
})

//登录
const onSubmit = ()=>{
  console.log("登录")
  //表单校验
  formRef.value.validate((valid)=>{
    if(!valid){
      console.log('表单校验不通过')
      return false
    }
  })
  loading.value = true
  login(form.username,form.password).then((res)=>{
    console.log('登录信息===》',res)
    if(res.success === true){
      /*ElMessage({
        message: '登录成功',
        type: 'success'
      })*/


      //存储token到cookie
      let  token = res.data.token
      setToken(token)
      // 获取用户信息，并存储到全局状态中
      userStore.setUserInfo()

      showMessage("登录成功","success")
      router.push('/admin/index')
    }else {
      //获取服务器返回错误信息
      let message = res.message
      showMessage(message, 'error')
    }
  }).finally(()=>{
    loading.value= false
  })
}




</script>
<style >

</style>
